package com.liuyang.service.impl;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuyang.entity.Account;
import com.liuyang.mapper.AccountMapper;
import com.liuyang.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者:caleb
 * @since 2022-02-22
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Map<String, Object> queryAccountManage(String accountName, int page, int limit) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<Account>();
        if(!StrUtil.isEmpty(accountName)) {
            queryWrapper.eq("accountName", accountName);
        }
        Page<Account> accountPage = new Page<>(page, limit);
        IPage<Account> accountIPage = accountMapper.selectPage(accountPage, queryWrapper);
        long count = accountIPage.getTotal();
        List<Account> list = accountIPage.getRecords();
        MapBuilder<String, Object> mapBuilder = MapBuilder.create();
        Map<String, Object> map = mapBuilder.put("count", count).put("list", list).build();
        return map;
    }
}
